package cn.momoshenchi.controller;

import cn.momoshenchi.pojo.Book;
import cn.momoshenchi.pojo.Tips;
import cn.momoshenchi.util.AssertUtil;
import cn.momoshenchi.util.ResultInfo;
import cn.momoshenchi.service.TipsService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * @version : 1.0
 * @author: momoshenchi
 * @date: 2021/6/7 - 22:13
 */
@RestController
@RequestMapping("tips")
public class TipsController
{
    @Autowired
    private TipsService tipsService;

    @GetMapping("random")
    @Operation(summary = "随机返回小知识")
    public ResultInfo random()
    {
        Tips tips = tipsService.getRandomOne();
        return new ResultInfo(tips);
    }


    @PostMapping("insert")
    @Operation(summary = "添加小知识")
    public ResultInfo insert(@RequestBody Tips tips) throws IOException
    {
        tipsService.insertWithEs(tips);
        return new ResultInfo();
    }

    @PostMapping("insertList")
    @Operation(summary = "批量添加小知识")
    public ResultInfo insertList(@RequestBody List<Tips> tipsList) throws IOException
    {
        tipsService.insertListWithEs(tipsList);
        return new ResultInfo();
    }

    @PostMapping("update")
    @Operation(summary = "更新小知识")
    public ResultInfo update(@RequestBody Tips tips)
    {
        AssertUtil.isTrue(!tipsService.updateById(tips), "更新失败");
        return new ResultInfo();
    }
}
